package com.zhongkai.service.impl;

import com.zhongkai.model3.*;
import org.codehaus.jackson.type.TypeReference;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.app.service.AddressService;
import com.zhongkai.app.service.DeviceOperationService;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址获取
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl implements AddressService {

    @Autowired
    private JedisClient jedisClient;

    @Autowired
    private IBaseDao iBaseDao;

   //@Resource(name = "amqpTemplate")
/*    @Autowired
    private AmqpTemplate rabbitAdmin;*/
   // @Autowired(required = false)
/*   @Autowired
    private RocketMqProducer rocketMqProducer;*/


    @Autowired
    private DeviceOperationService deviceOperationService;
/*    @Autowired
    private RocketMqProducer rocketMqProducer;*/

    /**
     * 获取所有省份
     */
    @Override
    public ZhongkaiResult getProvince() {
       // rabbitAdmin.convertAndSend("springtest","rabbitmq和Spring的整合测试");
/*        try {
            byte[] bytes = "大哥".getBytes("UTF-8");
            rocketMqProducer.sendMsgIntime("NETTY_SKY",
                    "NETTY_SKY",
                    bytes);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
;

     //   deviceOperationService.deviceDown2("19002a3437470839343238");
/*        String json = (String) jedisClient.get(StringStatic.REDIS_UNIT + "19002a3437470839343238"); //查询缓存
        System.out.println(json);*/



        try {

            String province = jedisClient.get("Province");
            ObjectMapper mapper = new ObjectMapper();

            if (!StringUtils.isNullOrEmpty(province)){
              /*  ArrayList<Province> menusList = mapper.readValues(province,new TypeReference);*/
                ArrayList<Province> menusList = mapper.readValue(province, new TypeReference<List<Province>>() {    //转成list
                });

                return ZhongkaiResult.ok(menusList);
            }

            List<Province> from_province = iBaseDao.find("from Province");

            String string = mapper.writeValueAsString(from_province);


            jedisClient.set("Province",string);
            return ZhongkaiResult.ok(from_province);

        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "查询失败");
        }


    }


    /**
     * 获取市
     *
     * @param province 省份id
     */
    @Override
    public ZhongkaiResult getCity(String province) {
        if (StringUtils.isNullOrEmpty(province)) {
            return ZhongkaiResult.build(501, "参数不完整");
        }
        try {
            List<City> cities = iBaseDao.find("from City ct where ct.provinceId='" + province + "'");
            return ZhongkaiResult.ok(cities);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "查询失败");
        }

    }

    /**
     * 获取区
     *
     * @param city 市id
     */
    @Override
    public ZhongkaiResult getCountry(String city) {
        if (StringUtils.isNullOrEmpty(city)) {
            return ZhongkaiResult.build(501, "参数不完整");
        }
        try {
            List<Country> cities = iBaseDao.find("from Country ct where ct.cityId='" + city + "'");
            return ZhongkaiResult.ok(cities);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "查询失败");
        }
    }

    /**
     * 获取乡
     *
     * @param country 地区id
     */
    @Override
    public ZhongkaiResult getTown(String country) {
        if (StringUtils.isNullOrEmpty(country)) {
            return ZhongkaiResult.build(501, "参数不完整");
        }
        try {
            List<Town> towns = iBaseDao.find("from Town tw where tw.countryId='" + country + "'");
            return ZhongkaiResult.ok(towns);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "查询失败");
        }
    }

    @Override
    public ZhongkaiResult getMyAddress(String id) {
        if ("1".equals(id)){
            List<Province> from_province = iBaseDao.find("from Province");
            return ZhongkaiResult.ok(from_province);
        }else if ("2".equals(id)){
            List<City> cities = iBaseDao.find("from City");
            return ZhongkaiResult.ok(cities);
        }else if ("3".equals(id)){
            List<Country> cities = iBaseDao.find("from Country");
            return ZhongkaiResult.ok(cities);
        }
        return ZhongkaiResult.ok();
    }


}
